finp$="picture.png"
'- original qbasic version - 0112141818
'- sdlbasic version - 0405111338
'- halftone picture converter (bmp 4/8/24bit)
'xed=0:yed=0:open finp$ for input as #1:for i=1 to 16:n=readbyte(1):next:for i=1 to 2:n=readbyte(1):next
'xed=readbyte(1)*256:xed=xed+readbyte(1):for i=1 to 2:n=readbyte(1):next:yed=readbyte(1)*256:yed=yed+readbyte(1)
'close #1
'setdisplay(xed,yed,32,1):paper(8^8-1):ink(0):pen(0):cls:

xed=640:yed=400
setdisplay(640,400,32,1):paper(8^8-1):ink(0):pen(0):cls
'- opens the image
loadimage(finp$,1):pasteicon(0,0,1)
dim clust[8,8] '-,o4b[2]
'-- define cluster
' a$="00,12,03,15,08,04,11,07,02,14,01,13,10,06,09,05"
' a$="00,06,08,14,02,12,04,10,08,14,00,06,04,10,02,12"
a$="00,08,06,14,00,08,06,14,04,12,02,10,04,12,02,10"
for y2=0 to 3:for x2=0 to 3:lc=(y2*4)+x2
  clust[x2,y2]=val(mid$(a$,(lc*3)+1,2)):next:next
dim cor[16]
cor[0]=0x000000:cor[1]=0x0000FF:cor[2]=0xFF0000:cor[3]=0xFF00FF
cor[4]=0x00FF00:cor[5]=0x00FFFF:cor[6]=0xFFFF00:cor[7]=0xFFFFFF
for y=yed-1 to 0 step -1
  for x=xed-1 to 0 step -1
    ind=point(x/2,y/2)
    b=ind mod 256:ind=int(ind/256)
    g=ind mod 256:ind=int(ind/256)
    r=ind mod 256
    k=int(((b*11)+(r*30)+(g*59))/100)
    patgf=(((clust[x mod 4,y mod 4]+1)*255)/16)
    'rpat=0:if r>patgf then:rpat=1:end if
    'gpat=0:if g>patgf then:gpat=1:end if
    'bpat=0:if b>patgf then:bpat=1:end if
    kpat=0:if k>patgf then:kpat=1:end if
    
    'o4b=(bpat+(rpat*2)+(gpat*4)) mod 8
    o4b=kpat*7

    ink(cor[o4b]):dot(x,y)
    next:next 
'- saves the image
fout$=finp$+".bmp"
grab (1,0,0,xed,yed):saveimage(fout$,1)
shell ("convert "+finp$+".bmp "+finp$+".png")
shell ("rm "+finp$+".bmp")
